﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using System.Threading;
using System.Windows.Threading;

namespace XPSReaderWPF
{
    /// <summary>
    /// Interaction logic for ChooseXPS.xaml
    /// </summary>
    public partial class ChooseXPS : Window
    {
        string _filename;

        #region FileName
        /// <summary>
        /// Gets or sets the filename.
        /// </summary>
        /// <value>The filename.</value>
        public string Filename
        {
            get { return _filename; }
            set { _filename = value; }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="ChooseXPS"/> class.
        /// </summary>
        public ChooseXPS()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Handles the Click event of the btBrowse control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btBrowse_Click(object sender, RoutedEventArgs e)
        {
            string name = "";
            string[] tabFilename;
            char[] separator = new char[2];
            int nbPage = 0;
            double pageHeight;
            double pageWidth;
            int cptExist = 0;
            XPSConversion p;
            Reader book;
            Loading load;

            separator[0] = '\\';
            separator[1] = '.';

            // OpenFileDialog Control to choose a XPS document
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = "XPS Document|*.xps";

            if (dlg.ShowDialog() == true)
            {
                Filename = dlg.FileName;
                tabFilename = Filename.Split(separator);

                // the file name without extension
                name = tabFilename[tabFilename.Count() - 2];

                try
                {
                    this.Visibility = Visibility.Hidden;

                    // Check if PNG files exists
                    if (!File.Exists(name + ".Page" + cptExist + ".png"))
                    {
                        // Window to wait during the XPS convertion
                        load = new Loading();
                        load.Show();

                        // Convert XPS pages to PNG files
                        p = new XPSConversion(Filename);
                        nbPage = p.SaveXpsPageToBitmap(Filename, name);
                        pageHeight = p.PageHeight;
                        pageWidth = p.PageWidth;
                        load.Close();
                    }
                    else
                    {
                        // Find the number of PNG files
                        Image pageFile = new Image();
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new Uri(name + ".Page" + cptExist + ".png", UriKind.RelativeOrAbsolute);
                        bi.EndInit();

                        pageFile.Source = bi;

                        pageHeight = pageFile.Source.Height;
                        pageWidth = pageFile.Source.Width;

                        while (File.Exists(name + ".Page" + cptExist + ".png"))
                        {
                            cptExist++;
                        }
                        nbPage = cptExist--;
                    }

                    // Show the book with PNG files
                    book = new Reader(name, nbPage, pageHeight, pageWidth);
                    book.ShowDialog();
                }
                catch
                {
                    System.Windows.MessageBox.Show("There is a problem during the XPS document conversion.");
                    System.Windows.Application.Current.Shutdown();
                }

            }

        }

    }
}
